package middleware;

import java.lang.reflect.*;


public class Greeter {
    public static String greet(String cname, String program, String lname) {        
        String greet = "Hej " + lname;
        
        if (cname != null && program != null) {
            try {
                if (StringJavaCompiler.compileAndLoad(cname, program)) {
                    Class<?> classz = Class.forName(cname);
                    Method[] methods = classz.getMethods();
                    if (methods.length > 0) {
                        Method m = methods[0];
                        Object ret = m.invoke(null, new Object[] { lname });
                        greet = (String) ret;
                   }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        System.out.println(greet);
        return greet;
    }
}
